namespace cnVery.Cms
{
    using cnVery.Cms.Kernel;
    using cnVery.Utils;
    using cnVery.Data;
    using System;
    using System.Data;
    using System.Text;
    using System.Web.UI.WebControls;

    public class eventlogs
    {
        // Methods
        public static void Add(eventlog model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into eventlogs(");
            builder.Append("username,remark,time,ip,system");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("'" + model.username + "',");
            builder.Append("'" + model.remark + "',");
            builder.Append("'" + model.time + "',");
            builder.Append("'" + model.ip + "',");
            builder.Append("'" + model.system + "'");
            builder.Append(")");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }

        public static void Clear()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete eventlogs ");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }

        public static void Delete(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete eventlogs ");
            builder.Append(" where id=" + id);
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }

        public static void Delete(string ids)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete eventlogs ");
            builder.Append(" where id in(" + ids + ")");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }

        public static bool Exists(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from eventlogs where id=" + id + "");
            Connection connection = new Connection();
            object scalar = connection.GetScalar(builder.ToString());
            connection.Close();
            if (object.Equals(scalar, null) || object.Equals(scalar, DBNull.Value))
            {
                return false;
            }
            int num = (int)scalar;
            return (num > 0);
        }

        public static int GetCount(string filter)
        {
            if (filter == "")
            {
                filter = "0=0";
            }
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from eventlogs where " + filter);
            Connection connection = new Connection();
            object scalar = connection.GetScalar(builder.ToString());
            connection.Close();
            if (object.Equals(scalar, null) || object.Equals(scalar, DBNull.Value))
            {
                return 0;
            }
            return (int)scalar;
        }

        public static DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from eventlogs ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by id desc");
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(builder.ToString());
            connection.Close();
            return dataTable;
        }

        public static DataTable GetList(int pageindex, int pagesize, string filter)
        {
            if (pageindex <= 0)
            {
                return null;
            }
            if (filter == "")
            {
                filter = "0=0";
            }
            string sql = string.Concat(new object[] { "select top ", pagesize, " * from eventlogs where ", filter });
            if (pageindex > 1)
            {
                object obj2 = sql;
                sql = string.Concat(new object[] { obj2, " and id < (select min(id) from (select top ", pagesize * (pageindex - 1), " id from eventlogs where ", filter, " order by id desc) as tb)" });
            }
            sql = sql + " order by id desc ";
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(sql);
            connection.Close();
            return dataTable;
        }

        public static eventlog GetModel(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from eventlogs ");
            builder.Append(" where id=" + id);
            eventlog eventlog = new eventlog();
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(builder.ToString());
            connection.Close();
            eventlog.id = id;
            if (dataTable.Rows.Count > 0)
            {
                eventlog.username = dataTable.Rows[0]["username"].ToString();
                eventlog.remark = dataTable.Rows[0]["remark"].ToString();
                eventlog.time = dataTable.Rows[0]["time"].ToString();
                eventlog.ip = dataTable.Rows[0]["ip"].ToString();
                eventlog.system = dataTable.Rows[0]["system"].ToString();
                return eventlog;
            }
            return null;
        }

        public static void Update(eventlog model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update eventlogs set ");
            builder.Append("username='" + model.username + "',");
            builder.Append("remark='" + model.remark + "',");
            builder.Append("time='" + model.time + "',");
            builder.Append("ip='" + model.ip + "',");
            builder.Append("system='" + model.system + "'");
            builder.Append(" where id=" + model.id + "");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }
    }
}

